//2009/08/24 13:28:43
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <sstream>
#include <algorithm>

using namespace std;

class SerialNumbers
{
public:
    vector <string> sortSerials(vector <string> serialNumbers)
    {
        vector<string> ret(serialNumbers);
        for (int i=0; i<ret.size(); i++)
            for (int j=0;j<ret.size();j++)
            {
                if (i!=j && cmp(ret[i], ret[j]) < 0)
                {
                    cout << ret[i] << " " << ret[j] << endl;
                    string temp = ret[j];
                    ret[j] = ret[i];
                    ret[i] = temp;
                    cout << ret[i] << " " << ret[j] << endl;
                }
            }
        return ret;
    }
private:
    int cmp(string a, string b)
    {
        int s1 = sum_of_digits(a);
        int s2 = sum_of_digits(b);
        //cout << "Sum: " << s1 << " " << s2 << endl;
        if (a.size() != b.size()) return a.size() - b.size();
        else if (s1 != s2) return s1 - s2;
        else return a.compare(b);
    }
    int sum_of_digits(string a)
    {
        int sum = 0;
        for (int i=0; i<a.size(); i++)
            if (isdigit(a[i]))
                sum += a[i] - '0';
        return sum;
    }
};
